package com.neusoft.actor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.actor.dao.CaretakerClientDao;
import com.neusoft.actor.dao.ClientDao;
import com.neusoft.actor.po.CaretakerClient;
import com.neusoft.actor.po.Client;
import com.neusoft.actor.po.ResponseBean;
import com.neusoft.actor.service.CaretakerClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CaretakerClientServiceImpl implements CaretakerClientService {

    @Autowired
    private CaretakerClientDao caretakerClientDao;

    @Autowired
    private ClientDao clientDao;

    @Override
    public ResponseBean<List<CaretakerClient>> getAllCaretakerClients() {
        List<CaretakerClient> caretakerClients = caretakerClientDao.selectList(null);
        return new ResponseBean<>(caretakerClients);
    }

    @Override
    public ResponseBean<CaretakerClient> getCaretakerClientById(Integer relationId) {
        CaretakerClient caretakerClient = caretakerClientDao.selectById(relationId);
        if (caretakerClient != null) {
            return new ResponseBean<>(caretakerClient);
        } else {
            return new ResponseBean<>(404, "健康管家与客户关系未找到");
        }
    }

    @Override
    public ResponseBean<Integer> addCaretakerClient(Integer clientId, Integer caretakerId) {
        CaretakerClient caretakerClient = new CaretakerClient();
        caretakerClient.setClientId(clientId);
        caretakerClient.setCaretakerId(caretakerId);
        Integer result = caretakerClientDao.insert(caretakerClient);
        return new ResponseBean<>(result); // 返回插入成功的行数
    }

    @Override
    public ResponseBean<Integer> updateCaretakerClient(CaretakerClient caretakerClient) {
        int result = caretakerClientDao.updateById(caretakerClient);
        return new ResponseBean<>(result); // 返回更新成功的行数
    }

    @Override
//    移除客户管家
    public ResponseBean<Integer> deleteCaretakerClient(Integer clientId) {
        CaretakerClient caretakerClient = caretakerClientDao.selectOne(new QueryWrapper<CaretakerClient>().eq("client_id", clientId));
        int result = caretakerClientDao.deleteById(caretakerClient);
        return new ResponseBean<>(result); // 返回删除成功的行数
    }

    @Override
//    根据健康管家id查询健康管家管理的客户,可以根据姓名模糊查询
    public ResponseBean<List<Client>> getCaretakerClientByCaretakerId(Integer id, String clientName) {
        // 查询健康管家管理的所有客户ID
        List<CaretakerClient> caretakerClients = caretakerClientDao.selectList(new QueryWrapper<CaretakerClient>().eq("caretaker_id", id));
        List<Integer> clientIds = caretakerClients.stream()
                .map(CaretakerClient::getClientId)
                .collect(Collectors.toList());

        // 如果没有客户，直接返回404
        if (clientIds.isEmpty()) {
            return new ResponseBean<>(404, "该健康管家没有客户");
        }

        // 构建查询条件
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("client_id", clientIds); // 限制在健康管家管理的客户范围内

        // 如果提供了客户名称，则添加模糊查询条件
        if (clientName != null && !clientName.isEmpty()) {
            queryWrapper.like("client_name", clientName);
        }

        // 查询符合条件的客户
        List<Client> clients = clientDao.selectList(queryWrapper);

        // 返回结果
        if (!clients.isEmpty()) {
            return new ResponseBean<>(clients);
        } else {
            return new ResponseBean<>(404, "未找到符合条件的客户");
        }
    }
}